package com.buobao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buobao.entity.MemberReadState;
import com.buobao.mapper.MemberReadStateMapper;
import com.buobao.service.MemberReadStateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("memberReadStateService")
public class MemberReadStateServiceImpl implements MemberReadStateService {
    @Resource
    private MemberReadStateMapper mapper;

    @Override
    public MemberReadState selectByMemberIdAndBookId(Long memberId, Long bookId) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("book_id",bookId);
        MemberReadState memberReadState = mapper.selectOne(queryWrapper);
        return memberReadState;
    }

    @Transactional
    @Override
    public MemberReadState insert(MemberReadState state) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",state.getMemberId());
        queryWrapper.eq("book_id",state.getBookId());
        MemberReadState memberReadState = mapper.selectOne(queryWrapper);
        if (memberReadState!=null) {
            mapper.update(state,queryWrapper);
        } else {
            mapper.insert(state);
        }
        return memberReadState;
    }

    @Transactional
    @Override
    public void delete(Long memberId, Long bookId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("member_id",memberId);
        mapper.delete(queryWrapper);
    }
}






















